#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hash_fun.h"
#include "hash.h"

int main()
{
    char buf[100];
    char str[100];
    struct elem_s *p;
    struct hashtb_s *hashtbp = NULL;
    //extern uint32_t djb2_hash(unsigned char *str, int size);
    //extern uint32_t djb1_hash(unsigned char *str, int size);
    //extern uint32_t sdbm_hash(unsigned char *str, int size);
    //extern uint32_t fnv1a_hash(unsigned char *str, int size);
    //extern uint32_t bkdr_hash(unsigned char *str, int size);
    //djb2 > djb1 >bkdr >sdbm >   fnv1a
    //49     39    35     29        25


    hashtbp = (struct hashtb_s*)hash_init(113330, hashtbp, bkdr_hash); 
    if(NULL == hashtbp)
    {
        perror("init error");
        exit(0);
    }
    FILE *fp;

    fp = fopen("data","a+");


    char *str_array[1024000];
    int  str_len[1024000];
    int str_num = 0;
    
    
    while(!feof(fp)&&NULL!=fgets(buf,99,fp))
    {
        sscanf(buf,"%s",str);
        //
        //
        p = (struct elem_s *)malloc(sizeof (struct elem_s));
        p->data = (void*)malloc(strlen(str)+1);
        memcpy((char*)(p->data),(char*)str,strlen(str));
        p->data_len = strlen(str);
        if (str_num < 1024000) {
            str_array[str_num] = p->data;
            str_len[str_num] = p->data_len;
            str_num++;
        }
        
        if (hash_search(p,hashtbp))
        {
            //printf("elem has exits\n");
            free(p);
            
        }
        else
        {
            hash_insert(p,hashtbp);
        }
    }

    time_t start = time(NULL);
    printf("start time %u\n", (uint32_t)start);
    int i=0;
    int j = 0;
    p = (struct elem_s *)malloc(sizeof (struct elem_s));
    for (i=0;i<10*(str_num-1); i++) {
        j = random() % str_num;
        p->data = str_array[j];
        p->data_len = str_len[j];
        hash_search(p, hashtbp);
    }
    time_t end = time(NULL);
    printf("start time %u\n", (uint32_t)end);
    printf("spend time %u\n", ((uint32_t)end) - ((uint32_t)start));
    return 0;
}
